home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / emulator / uae-0.000 / uae-0 / uae-0.6.0 / gfxutil.c < prev    next >
C/C++ Source or Header  |  1996-05-04  |  3KB  |  158 lines

  1.  /* 
  2.   * UAE - The Un*x Amiga Emulator
  3.   * 
  4.   * Common code needed by all the various graphics systems.
  5.   * 
  6.   * (c) 1996 Bernd Schmidt, Ed Hanway
  7.   */
  8.  
  9. #include "sysconfig.h"
  10. #include "sysdeps.h"
  11.  
  12. #include "config.h"
  13. #include "options.h"
  14. #include "memory.h"
  15. #include "custom.h"
  16. #include "newcpu.h"
  17. #include "keyboard.h"
  18. #include "xwin.h"
  19. #include "keybuf.h"
  20.  
  21. unsigned long doMask(int p, int bits, int shift)
  22. {
  23.     /* p is a value from 0 to 15 (Amiga color value)
  24.      * scale to 0..255, shift to align msb with mask, and apply mask */
  25.  
  26.     unsigned long val = p * 0x11111111UL;
  27.     val >>= (32 - bits);
  28.     val <<= shift;
  29.  
  30.     return val;
  31. }
  32.  
  33. void alloc_colors64k(int rw, int gw, int bw, int rs, int gs, int bs)
  34. {
  35.     int i;
  36.     for(i=0; i<4096; i++) {
  37.     int r = i >> 8;
  38.     int g = (i >> 4) & 0xF;
  39.     int b = i & 0xF;
  40.     xcolors[i] = doMask(r, rw, rs) | doMask(g, gw, gs) | doMask(b, bw, bs);
  41.     }
  42. }
  43.  
  44. static int allocated[4096];
  45.  
  46. void alloc_colors256(int (* allocfunc)(int, int, int, xcolnr *))
  47. {
  48.     /* This is kind of kludgy...
  49.      * Try to allocate as many different colors as possible. */
  50.     int step = 5, mode = 0, count = 0, col;
  51.     int no_allocated = 0;
  52.  
  53.     memset(allocated, 0, sizeof allocated);
  54.     fprintf(stderr, "Allocating lots of colors...\n");
  55.     
  56.     do {
  57.     int r, g, b, rs, bs, gs;
  58.     bs = step;
  59.     rs = step;
  60.     gs = step;
  61.     for(b = 0; b < 16; b += bs) {
  62.         for(r = 0; r < 16; r += rs) {
  63.         for(g = 0; g < 16; g += gs) {
  64.             int cnr = (r << 8) + (g << 4) + b;
  65.             if (!allocated[cnr]) {
  66.             int result = allocfunc(r, g, b, xcolors + cnr);
  67.             if (result < 0 || no_allocated == 256)
  68.                 goto finished;
  69.             if (result > 0) {
  70.                 allocated[cnr] = 1;
  71.                 no_allocated++;
  72.             }
  73.             }
  74.         }
  75.         }
  76.     }
  77.     if (step > 1)
  78.         step++;
  79.     step /= 2;
  80.     } while (step);
  81.     
  82.     finished:
  83. #if 0
  84.     for(col = 0; col < 4096; col++) {
  85.     int cnr = col;
  86.     if (!allocated[cnr]) {
  87.         int r = cnr >> 8;
  88.         int g = (cnr >> 4) & 0xF;
  89.         int b = cnr & 0xF;
  90.         int maxd = 4096,best = 0;
  91.         int c2;
  92.         for(c2 = 0; c2 < 4096; c2++)
  93.             if (allocated[c2]) {
  94.             int r2 = c2 >> 8;
  95.             int g2 = (c2 >> 4) & 0xF;
  96.             int b2 = c2 & 0xF;
  97.             int rd = (r2-r)*r*2;
  98.             int bd = (b2-b)*b*2;
  99.             int gd = (g2-g)*g*2;
  100.             int dist = abs(rd) + abs(gd) + abs(bd) + abs(rd-gd)/3 + abs(rd-bd)/3 + abs(gd-bd)/3;
  101.             if (dist < maxd) {
  102.             maxd = dist;
  103.             best = c2;
  104.             }
  105.         }
  106.         cnr = best;
  107.     }
  108.     xcolors[col] = xcolors[cnr];
  109.     }
  110. #else
  111.     for(col = 0; col < 4096; col++) {
  112.     int cnr;
  113.     int dist;
  114.     int r = col >> 8;
  115.     int g = (col >> 4) & 0xF;
  116.     int b = col & 0xF;
  117.     
  118.     if (allocated[col])
  119.         continue;
  120.     
  121.     for (dist = 2;; dist++) {
  122.         int r2, g2, b2;
  123.         int rd, gd, bd;
  124.         int best = 0, maxd = 32767;
  125.         for (gd = 0; gd < dist; gd = gd < 0 ? -gd : -gd-1) {
  126.         g2 = g + gd;
  127.         if (g2 < 0 || g2 > 15)
  128.             continue;
  129.         for (rd = 0; rd < dist; rd = rd < 0 ? -rd : -rd-1) {
  130.             r2 = r + rd;
  131.             if (r2 < 0 || r2 > 15)
  132.             continue;
  133.             for (bd = 0; bd < dist; bd = bd < 0 ? -bd : -bd-1) {
  134.             b2 = b + bd;
  135.             if (b2 < 0 || b2 > 15)
  136.                 continue;
  137.             cnr = (r2 << 8) + (g2 << 4) + b2;
  138.             if (allocated[cnr]) {
  139.                 int rd2 = rd == 0 ? 0 : rd*32/r;
  140.                 int gd2 = gd == 0 ? 0 : gd*32/g;
  141.                 int bd2 = bd == 0 ? 0 : bd*32/b;
  142.                 int dist = abs(rd) + abs(gd) + abs(bd) + abs(rd2-gd2) + abs(rd2-bd2) + abs(gd2-bd2);
  143.                 if (dist < maxd) {
  144.                 maxd = dist;
  145.                 xcolors[col] = xcolors[cnr];
  146.                 }
  147.             }
  148.             }
  149.         }
  150.         }
  151.         if (maxd != 32767)
  152.         break;
  153.     }
  154.     }
  155. #endif
  156. }
  157.  
  158.